<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/3/7
 * Time: 下午2:33
 */
class Index extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->helper('url');
        $this->load->model('user_model');
        $this->load->model('goods_model');
        $this->load->model('admin_model');
        $this->load->model('bill_model');
        $this->load->model('upload_model');
        $this->load->model('customer_model');
        $this->load->model('budget_model');
        $this->load->library('session');
    }

    public function upload_test()
    {
        $path = $this->config->item("upload_path");
        $this->upload_model->do_upload('test', $path);
    }

    // 上传图片
    public function upload_contract()
    {
        $max_size = 20480;
        $path = $this->config->item("upload_path");
        $data = $this->upload_model->setType('compress')->do_upload('file', $path, true, true, $max_size);
        if ($data['status'] == 0) {
            $customer_id = $this->session->customer_id;
            $this->db->trans_begin();
            $bool1 = $this->db->where(array("customer_id" => $customer_id))->update('app_company', array('contract_pic_path' => $data['data']['upload_data']['file_name']));
            $bool2 = $this->db->where(array("customer_id" => $customer_id))->update('app_admin', array('account_period_status' => 4));
            $bool3 = $this->db->insert("app_account_period_apply", array('customer_id' => $customer_id, 'type' => 2, 'created_at' => date('Y-m-d H:i:s')));
            if ($bool1 && $bool2 && $bool3) {
                $this->db->trans_commit();
                $this->session->set_flashdata('msg', '提交成功');
            } else {
                $this->db->trans_rollback();
                $this->session->set_flashdata('alert', '提交失败，请联系您的销售经理或稍后再试！');
            }
        } else {
            $this->session->set_flashdata('alert', '上传失败!' . $data['data']['error']);
        }
        redirect();
    }


    public function interface_test()
    {
        $post = $this->input->post();
        $customer_id = $this->session->customer_id;
        $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
        $ews = $this->goods_model->company_office_supply($admin_shop_user_id);
    }

    //工作台首页
    public function index()
    {
        log_message('error', 'index');

        $data = '';
        $customer_id = $this->session->customer_id;
        $identity = $this->session->customer_info['identity'];
        //公告
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_notice/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['pages'] = 1;
        $rs = curl_request($url, $data_arr);
        $date = json_decode($rs, true);
        if ($date['code'] == 200) {
            $data['datas'] = $date['data']['data'];
        }
        //优质办公
        if($customer_id){
            $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
        }else{
            $admin_shop_user_id = "";
        }
        
        $data['datass'] = $this->goods_model->company_office_supply($admin_shop_user_id);
        
        if ($customer_id) {
            $customer = $this->db->select('c.identity,c.shop_user_id')->where('id', $customer_id)->from('app_customer c')->get();
            $customer = $customer->row_array();
            // 客户信息
            $data['customer_info'] = $this->customer_model->query_customer_info($customer_id);
            $data['admin_info'] = $this->admin_model->query_admin_info($customer_id);
            $data['period_apply_info'] = $this->customer_model->query_app_account_period_apply($customer_id);
            $month = date('Y-m', time());
            $data['month'] = $month;
            // 总预算
            $data['budget_info'] = $this->budget_model->query_budget_info($month, $customer_id);
            //单品额度
            $data['single_quota'] = $this->customer_model->u_m_single_quota_q($customer_id);
            if ($customer['identity'] == 1) {
                // 账期信息
                if ($data['admin_info']['is_account']) {
                    if ($data['admin_info']['account_period_type'] == 1) {
                        $data['bill_info'] = $this->bill_model->query_bill_month_by_month($customer_id, $month, '(0,2)');
                    } else {
                        $data['bill_info']['should_repayment_date'] = $this->bill_model->query_bill_time_date($customer_id);
                        $data['bill_info']['money'] = $this->bill_model->query_bill_time_money($customer_id);
                    }
                }
            }

            //用户信息
            if ($identity == 1 || $identity == 2) {
                $parent_id = $this->customer_model->get_customer_user($customer_id);
                $data['user'] = $this->db->from('app_customer')->select('username,id,parent_id')->where('identity', 3)->where_in('parent_id', $parent_id)->get()->result_array();
                $str = $this->customer_model->get_customer_user($customer_id, 1);
                if (count($str) != 0) {
                    $this->db->where_in('customer_id', $str);
                    $this->db->where('status', 2);
                }
                $count1 = $this->db->from('app_temp')->count_all_results();
                $data['manager_approve_order'] = $count1;
                if (count($str) != 0) {
                    $this->db->where_in('customer_id', $str);
                    $this->db->where('status', 4);
                }
                $count2 = $this->db->from('app_temp')->count_all_results();
                $data['admin_approve_order'] = $count2;
            }

            if ($identity == 1) {
                $data['delayed_bill'] = $this->bill_model->get_delayed_bill($customer_id);
                $company = $this->db->select('cu.company')->where('cu.id', $customer_id)->from('app_customer cu')->get();
                $company = $company->row_array();

                $sql = "SELECT username,id  FROM app_customer WHERE identity = 2 AND parent_id = $customer_id";
                $data['manage'] = $this->db->query($sql)->result_array();


                $data['company'] = $company;
//                print_r(  $data['admin_count']);
                $company_table = $this->db->select('*')->where('customer_id', $customer_id)->from('app_company')->get();
                $data['company_data'] = $company_table->row_array();

                if ($data['customer_info']['manager_id']) {
                    $manager_table = $this->db->select('*')->where('manager_id', $data['customer_info']['manager_id'])->from('admin_manager')->get();
                    $data['manager_data'] = $manager_table->row_array();
                } else {
                    $data['manager_data'] = null;
                }
            } else {
                $cost = $this->db->select('cu.*,co.name as coname,cut.username as cutname,cut.email as cutemail')
                    ->where('cu.id', $customer_id)->from('app_customer cu')
                    ->join('app_customer cut', 'cut.id=cu.parent_id')
                    ->join('app_cost_center co', 'co.id=cu.cost_center_id')->get();
                $cost = $cost->row_array();
                $data['cost'] = $cost;
            }

            //查询审批
            $temp = $this->db->select('app_temp.status')->where('customer_id', $customer_id)->from('app_temp')->get();
            $temp = $temp->result_array();
            $data['temp'] = $temp;
            $status = array();
            $status1 = array();
            $status2 = array();
            $status3 = array();
            if (count($temp)) {
                foreach ($temp as $key => $tem) {
                    if ($tem['status'] == 0) {         //取消
                        $status[$key] = $tem['status'];
                    }
                    if ($tem['status'] == 1) {   //通过
                        $status1[$key] = $tem['status'];
                    }
                    if ($tem['status'] == 2 || $tem['status'] == 4) {      //申请中
                        $status2[$key] = $tem['status'];
                    }
                    if ($tem['status'] == 3 || $tem['status'] == 5) {   //驳回
                        $status3[$key] = $tem['status'];
                    }
                }
            }
            //查询待付款
            $orders = array();
            $order = $this->db->select('app_order.status')->where('customer_id', $customer_id)->from('app_order')->get();
            $order = $order->result_array();
            if (count($order)) {
                foreach ($order as $key => $tem) {
                    if ($tem['status'] == 4) {         //待付款
                        $orders[$key] = $tem['status'];
                    }
                }
            }

            $data['order'] = $orders;
            $data['status'] = $status;
            $data['status1'] = $status1;
            $data['status2'] = $status2;
            $data['status3'] = $status3;
            $data['identity'] = $identity;

            if ($identity == 1) {
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
            }
            if ($identity == 2) {
                $parent_id = $this->user_model->customer_id_to_parent_id($customer_id);
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($parent_id);
            }
            if ($identity == 3) {
                $parent_id = $this->user_model->customer_id_to_admin_id($customer_id);
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($parent_id);
            }

            //user/manage限购清单
            $list_id = $this->customer_model->query_customer_list_ids($customer_id);
            $lists = $this->customer_model->query_customer_list($shop_user_id, $list_id);

            if (!empty($lists)) {
                if ($lists['code'] == 200) {
                    $data['goods'] = $lists['data'];
                    $data['goodstotal'] = count($lists['data']);
                } else {
                    $data['goods'] = array();
                    $data['goodstotal'] = 0;
                }
            } else {
                $data['goods'] = array();
                $data['goodstotal'] = 0;
            }


            // admin限购商品
            $product = $this->customer_model->query_customer_list_product($shop_user_id);

            if (!empty($product)) {
                if ($product['status'] == 'success') {
                    if (count($product['data']['list'])) {
                        $ids = array();
                        foreach ($product['data']['list'] as $k => $v) {
                            $ids[] = $v['product_id'];
                        }
                        //优质办公
                        if($customer_id){
                            $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
                        }else{
                            $admin_shop_user_id = "";
                        }
                        $data['products'] = $this->goods_model->company_goods_infoes(implode(',', $ids),$admin_shop_user_id);

                    } else {
                        $data['products'] = array();
                    }

                }
            } else {
                $data['products'] = array();
            }

            $data['anonymous'] = false;
        } else {
            $data['anonymous'] = true;
        }
        $this->load->view('index/index.php', $data);
    }

    public function index_user()
    {
        $customer_id = $this->session->customer_id;
        $url = $this->config->item("shop_url") . 'companymanagerapi/company_notice/';
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $data_arr['pages'] = 1;
        $rs = curl_request($url, $data_arr);
        $date = json_decode($rs, true);
        if ($date['code'] == 200) {
            $r_info['datas'] = $date['data']['data'];
        }
        //优质办公
        if($customer_id){
            $admin_shop_user_id = $this->user_model->customer_id_to_admin_to_shop_user_id($customer_id);
        }else{
            $admin_shop_user_id = "";
        }
        $r_info['datass'] = $this->goods_model->company_office_supply($admin_shop_user_id);
        if ($customer_id) {
            $query = $this->db->query("SELECT * FROM app_manager_user WHERE customer_id=" . $customer_id);
            $r_info = $query->row_array();
            $r_info['data'] = $r_info;
            $budget = $this->db->query("SELECT * FROM app_budget WHERE customer_id=" . $customer_id);
            $budgets = $budget->row_array();
            $r_info['budget'] = $budgets;
        }
        $this->load->view('index/index_user.php', $r_info);
    }

    //额度申请
    public function index_budget()
    {
        $customer_id = $this->session->customer_id;
        $month = date('Y-m', time());
        $data['month'] = $month;
        // 总预算
        $budget_info = $this->budget_model->query_budget_info($month, $customer_id);
        //单品额度
        $single_quota = $this->customer_model->u_m_single_quota_q($customer_id);
        $budget['total'] = $budget_info['budget'];
        $budget['single'] = $single_quota;
        if (ceil($budget_info['budget']) != 0 && ceil($single_quota) != 0) {
            $this->load->view('index/index_budget.php', $budget);
        } elseif (ceil($single_quota) == 0) {
            $this->load->view('index/index_budget_total.php', $budget);
        } elseif (ceil($budget_info['budget']) == 0) {
            $this->load->view('index/index_budget_single.php', $budget);
        }

    }

    //总预算额度申请
    public function index_budget_total()
    {
        $customer_id = $this->session->customer_id;
        if ($_POST) {
            $data['remark'] = $_POST['other'];
            $data['created_at'] = date('Y-m-d H:i:s', time());;
            $data['customer_id'] = $customer_id;
            if ($_POST['tatol']) {
                $data['expect_budget'] = $_POST['tatol'];
                $budget = $this->db->insert('app_adjust_budget', $data);
                if ($budget == true) {
                    //通知上级
                    $history = $this->budget_model->total_message_history($customer_id, $data);
                    if ($history == true) {
                        send_json(array('code' => 0, 'data' => '', 'msg' => "提交申请成功"));
                    } else {
                        send_json(array('code' => -1, 'data' => '', 'msg' => "提交申请失败"));
                    }
                }
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "请输入期望预算额度"));
            }

        }
    }

    //单品额度申请
    public function index_budget_singel()
    {
        $customer_id = $this->session->customer_id;
        $data['remark'] = $_POST['other'];
        $data['created_at'] = date('Y-m-d H:i:s', time());;
        $data['customer_id'] = $customer_id;

        if ($_POST['single']) {
            $data['expect_single_quote'] = $_POST['single'];
            $single = $this->db->insert('app_adjust_single_quota', $data);
            if ($single == true) {
                //通知上级
                $history = $this->budget_model->singel_message_history($customer_id, $data);
                if ($history == true) {
                    send_json(array('code' => 0, 'data' => '', 'msg' => "提交申请成功"));
                } else {
                    send_json(array('code' => -1, 'data' => '', 'msg' => "提交申请失败"));
                }
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "请输入期望单品额度"));
        }

    }

    //单品额度和总预算额度申请
    public function budget_singel_total()
    {
        $customer_id = $this->session->customer_id;
        if ($_POST['tatol']) {
            $data1['expect_budget'] = $_POST['tatol'];
            $data1['remark'] = $_POST['other'];
            $data1['created_at'] = date('Y-m-d H:i:s', time());;
            $data1['customer_id'] = $customer_id;
            if ($_POST['single']) {
                $data['remark'] = $_POST['other'];
                $data['created_at'] = date('Y-m-d H:i:s', time());;
                $data['customer_id'] = $customer_id;
                $data['expect_single_quote'] = $_POST['single'];
                $budget = $this->db->insert('app_adjust_budget', $data1);
                $single = $this->db->insert('app_adjust_single_quota', $data);
                if ($single && $budget) {
                    //通知上级
                    $history = $this->budget_model->singel_total_message_history($customer_id, $data1, $data);
                    if ($history == true) {
                        send_json(array('code' => 0, 'data' => '', 'msg' => "提交申请成功"));
                    } else {
                        send_json(array('code' => -1, 'data' => '', 'msg' => "提交申请失败"));
                    }
                }
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "请输入期望单品额度"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "请输入期望预算额度"));
        }


    }

    //Manager账户列表
    public function index_admin_manage()
    {
        $customer_id = $this->session->customer_id;
        $user_admin_id = $this->customer_model->get_admin_customer_id($customer_id);
        $uid = $this->user_model->customer_id_to_shop_user_id($user_admin_id);
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $url = $this->config->item("shop_url") . '/companymanagerapi/mPur_mylist/?time=' . $data_arr['time'] . '&key=' . $data_arr['key'] . '&uid=' . $uid;
        $rs = curl_request($url);
        $data_l = json_decode($rs, true);

        if ($customer_id) {
            $customer = $this->db->where(array('cut.parent_id' => $customer_id, 'identity' => 2))->from('app_customer cut')->get()->result_array();
            foreach ($customer as $k => $r) {
                $customer[$k]['budgets'] = $this->db->select('budget,used,month')->where(array('customer_id' => $r['id'], 'month' => date('Y-m')))->from('app_budget')->get()->row_array();
                $customer[$k]['manager_user'] = $this->db->select('single_quota,total_quota')->where('customer_id', $r['id'])->from('app_manager_user')->get()->row_array();
                $customer[$k]['cost_center'] = $this->db->select('name')->where('id', $r['cost_center_id'])->from('app_cost_center')->get()->row_array();
            }
            $r_info['admin'] = $customer;
            //限购清单
            $lists = array();
            foreach ($customer as $key => $v) {
                $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
                $list_id = $this->customer_model->query_customer_list_ids($v['id']);
                $lists[$v['id']] = array();
                if ($data_l['code'] == 200) {
                    if (count($data_l['data']) && count($list_id)) {
                        foreach ($list_id as $y) {
                            foreach ($data_l['data'] as $n) {
                                if ($n['id'] == $y['list_id']) {
                                    $lists[$v['id']][$n['id']] = $n['name'];
                                }
                            }
                        }
                    }
                }
                //$lists[$v['id']] = $this->customer_model->query_customer_list($shop_user_id, $list_id);
            }
            $r_info['datas'] = $lists;

            $this->load->view('index/index_admin_manage.php', $r_info);
        }
        if ($_POST) {
            $sql = "SELECT * FROM app_customer WHERE identity = 2 AND parent_id = $customer_id";
            if ($_POST['username']) {
                $name = $_POST['username'];
                $sql .= " and username like '%$name%' ";
            }
            $user_list = $this->db->query($sql)->result_array();
            //账户查询
            if (count($user_list)) {
                foreach ($user_list as $k => $r) {
                    $user_list[$k]['budgets'] = $this->db->select('budget,used,month')->where(array('customer_id' => $r['id'], 'month' => date('Y-m')))->from('app_budget')->get()->row_array();
                    $user_list[$k]['manager_user'] = $this->db->select('single_quota,total_quota')->where('customer_id', $r['id'])->from('app_manager_user')->get()->row_array();
                    $user_list[$k]['cost_center'] = $this->db->select('name')->where('id', $r['cost_center_id'])->from('app_cost_center')->get()->row_array();

                    $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
                    $list_id = $this->customer_model->query_customer_list_ids($r['id']);

                    $user_list[$k]['datas'] = array();
                    if ($data_l['code'] == 200) {
                        if (count($data_l['data']) && count($list_id)) {
                            foreach ($list_id as $y) {
                                foreach ($data_l['data'] as $n) {
                                    if ($n['id'] == $y['list_id']) {
                                        $user_list[$k]['datas'][$n['id']] = $n['name'];
                                    }
                                }
                            }
                        }
                    }
                    $user_list[$k]['totals'] = count($user_list[$k]['datas']);

//                    $datas = $this->customer_model->query_customer_list($shop_user_id, $list_id);
//
//                    if ($datas['code'] == 200) {
//                        $user_list[$k]['datas'] = $datas['data'];
//                        $user_list[$k]['totals'] = $datas['total'];
//                    } else {
//                        $user_list[$k]['datas'] = [];
//                        $user_list[$k]['totals'] = 0;
//                    }
                }
                send_json(array('code' => 0, 'data' => $user_list, 'msg' => ""));
            } else {
                send_json(array('code' => -1, 'data' => array(), 'msg' => ""));
            }
        }

    }


    //User账户列表
    public function index_user_manage()
    {
        $customer_id = $this->session->customer_id;
        $user_admin_id = $this->customer_model->get_admin_customer_id($customer_id);
        $uid = $this->user_model->customer_id_to_shop_user_id($user_admin_id);
        $data_arr['time'] = time();
        $data_arr['key'] = md5($this->config->item("token_const_wjyh") . $data_arr['time']);
        $url = $this->config->item("shop_url") . '/companymanagerapi/mPur_mylist/?time=' . $data_arr['time'] . '&key=' . $data_arr['key'] . '&uid=' . $uid;
        $rs = curl_request($url);
        $data_l = json_decode($rs, true);


        if ($customer_id) {
            $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);

            //用户查询
            $managers = $this->db->where(array('parent_id' => $customer_id, 'identity' => 2))->from('app_customer')->get()->result_array();
            $customer = array();
            $customer2 = array();
            if (count($managers)) {
                foreach ($managers as $manager) {
                    $children = $this->db->where(array('parent_id' => $manager['id'], 'identity' => 3))->from('app_customer')->get()->result_array();
                    if (count($children)) {
                        foreach ($children as $child) {
                            $customer2[] = $child;
                        }
                    }
                }
            }
            $customer1 = $this->db->where(array('parent_id' => $customer_id, 'identity' => 3))->from('app_customer')->get()->result_array();
            $customer = array_merge_recursive($customer2, $customer1);
            if (count($customer)) {
                foreach ($customer as $k => $r) {
                    if ($r['parent_id'] != $customer_id) {
                        $customer[$k]['pusername'] = $this->db->select('username')->where('id', $r['parent_id'])->from('app_customer')->get()->row_array();
                    } else {
                    }

                    $customer[$k]['budgets'] = $this->db->select('budget,used')->where(array('customer_id' => $r['id'], 'month' => date('Y-m')))->from('app_budget')->get()->row_array();

                    $customer[$k]['manager_user'] = $this->db->select('single_quota')->where('customer_id', $r['id'])->from('app_manager_user')->get()->row_array();
                    $customer[$k]['cost_center'] = $this->db->select('name')->where('id', $r['cost_center_id'])->from('app_cost_center')->get()->row_array();
                }
                $r_info['user'] = $customer;

                foreach ($customer as $k => $r) {
                    $list_id = $this->customer_model->query_customer_list_ids($r['id']);
                    //$lists[$r['id']] = $this->customer_model->query_customer_list($shop_user_id, $list_id);

                    $lists[$r['id']] = array();
                    if ($data_l['code'] == 200) {
                        if (count($data_l['data']) && count($list_id)) {
                            foreach ($list_id as $y) {
                                foreach ($data_l['data'] as $n) {
                                    if ($n['id'] == $y['list_id']) {
                                        $lists[$r['id']][$n['id']] = $n['name'];
                                    }
                                }
                            }
                        }
                    }
                }

                $r_info['datas'] = $lists;

            } else {
                $r_info['user'] = array();
                $r_info['datas'] = array();
            }

//            print_r($r_info);
            $this->load->view('index/index_user_manage.php', $r_info);


//            $parent_id = $this->customer_model->get_customer_user_parent($customer_id);
//
//            $lists = array();
//            foreach ($parent_id['str1'] as $key => $id) {
//                if ($id) {
////                    $shop_user_id = $this->db->select('c.shop_user_id')->where('id', $id)->from('app_customer c')->get()->row_array();
////                    $list_id = $this->db->select('c.list_id')->where('customer_id', $id)->from('app_customer_list c')->get()->result_array();
////                    $lists[$id] = $this->customer_model->query_customer_list($shop_user_id, $list_id);
//
//                    $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
//                    $list_id = $this->customer_model->query_customer_list_ids($id);
//                    $lists[$id] = $this->customer_model->query_customer_list($shop_user_id, $list_id);
//                }
//            }
//
//            $r_info['datas'] = $lists;
//            $this->load->view('index/index_user_manage.php', $r_info);
        }
        if ($_POST) {


            $sql = "SELECT * FROM app_customer WHERE identity = 3 ";
            if ($_POST['username']) {
                $name = $_POST['username'];
                $sql .= " and username like '%$name%' ";
            }
            $sql .= " and ( parent_id in (select id from app_customer where identity = 2 and parent_id = $customer_id  ) or parent_id = $customer_id)";

            $user_list = $this->db->query($sql)->result_array();
        

            if (count($user_list)) {
                //用户查询
                foreach ($user_list as $k => $r) {
                    if ($r['parent_id'] != $customer_id) {
                        $user_list[$k]['pusername'] = $this->db->select('username,id')->where('id', $r['parent_id'])->from('app_customer')->get()->row_array();
                    }
                    $user_list[$k]['budgets'] = $this->db->select('budget,used,month')->where('customer_id', $r['id'])->from('app_budget')->get()->row_array();
                    $user_list[$k]['manager_user'] = $this->db->select('single_quota,total_quota')->where('customer_id', $r['id'])->from('app_manager_user')->get()->row_array();
                    $user_list[$k]['cost_center'] = $this->db->select('name')->where('id', $r['cost_center_id'])->from('app_cost_center')->get()->row_array();
                    //限购清单
//                $shop_user_id = $this->db->select('c.shop_user_id')->where('id', $r['id'])->from('app_customer c')->get()->row_array();
//                $list_id = $this->db->select('c.list_id')->where('customer_id', $r['id'])->from('app_customer_list c')->get()->result_array();
//                $datas = $this->customer_model->query_customer_list($shop_user_id, $list_id);

                    $shop_user_id = $this->user_model->customer_id_to_shop_user_id($customer_id);
                    $list_id = $this->customer_model->query_customer_list_ids($r['id']);

                    $user_list[$k]['datas'] = array();
                    if ($data_l['code'] == 200) {
                        if (count($data_l['data']) && count($list_id)) {
                            foreach ($list_id as $y) {
                                foreach ($data_l['data'] as $n) {
                                    if ($n['id'] == $y['list_id']) {
                                        $user_list[$k]['datas'][$n['id']] = $n['name'];
                                    }
                                }
                            }
                        }
                    }
                    $user_list[$k]['totals'] = count($user_list[$k]['datas']);

//                    $datas = $this->customer_model->query_customer_list($shop_user_id, $list_id);
//
//                    if ($datas['code'] == 200) {
//                        $user_list[$k]['datas'] = $datas['data'];
//                        $user_list[$k]['totals'] = $datas['total'];
//                    } else {
//                        $user_list[$k]['datas'] = [];
//                        $user_list[$k]['totals'] = 0;
//                    }
                }

                send_json(array('code' => 0, 'data' => $user_list, 'msg' => ""));
            } else {
                send_json(array('code' => -1, 'data' => array(), 'msg' => ""));
            }
        }
    }


    // 激活页面
    public function index_activation()
    {
        $customer_id = $this->session->customer_id;
        if (!$customer_id) {
            redirect('index');
            exit();
        }
        $sql = 'select ac2.username,ac2.shop_user_id,ac2.company,ac2.name,ac2.identity,mu.activation from app_customer as ac1 LEFT JOIN app_manager_user as mu ON mu.customer_id = ac1.id LEFT JOIN app_customer as ac2 ON ac1.parent_id = ac2.id WHERE ac1.id = ' . $customer_id;
        $customer_info = $this->db->query($sql)->row_array();
        if ($customer_info['activation'] == 1) {
            redirect('index');
            exit();
        }
        $data['username'] = $this->session->customer_info['username'];
        $data['parent_name'] = $customer_info['username'];
        $this->load->view('index/index_activation.php', $data);
    }

    // 冻结页面
    public function index_freeze()
    {
        $customer_id = $this->session->customer_id;
        if (!$customer_id) {
            redirect('index');
            exit();
        }
        $data['username'] = $this->session->customer_info['username'];
        $this->load->view('index/index_freeze.php', $data);
    }

    //了解工作台
    public function index_desk()
    {
        $data = '';
        $customer_id = $this->session->customer_id;
        if ($customer_id) {
            $identity = $this->user_model->customer_id_to_identity($customer_id);
            if ($identity == 1) {
                $company = $this->db->select('cu.company')->where('cu.id', $customer_id)->from('app_customer cu')->get();
                $company = $company->row_array();
                $data['company'] = $company;
            }
        }
        if ($_POST) {
            if ($customer_id) {
                $data_add['company'] = $_POST['company'];
                if ($this->db->where('id', $customer_id)->update('app_customer', $data_add)) {
                    send_json(array('code' => 0, 'data' => $_POST['company'], 'msg' => '保存成功'));
                }
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "请先登录"));

            }

        }
        $this->load->view('index/index_desk', $data);
    }

}